package com.cpp.server.config;

import com.cpp.common.constant.AiPromptConstant;
import com.cpp.server.mapper.MessageMapper;
import com.cpp.server.memory.MysqlChatMemory;
import com.cpp.server.tools.*;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {
    //配置一个会话实现类InMemoryChatMemory基于Map
    //实现了ChatMemory接口，我也可以自己实现一个，实现方可以是redis mysql 数列
    //在111中的defaultAdvisors（）被使用，设置这个ai的记忆库
    @Bean
    public ChatMemory chatMemory(MessageMapper messageMapper) {
        return new MysqlChatMemory(messageMapper);
    }
//    vectorStore用的是redis

    //生成ppt大纲的ai
    @Bean
    public ChatClient generatepptTextChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore, GeneratePptTool generatePptTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.generate_ppt_text_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
//                        new MessageChatMemoryAdvisor(chatMemory),
                        new QuestionAnswerAdvisor(
                                vectorStore,//向量库
                                SearchRequest.builder()//搜索请求
                                        .topK(100)//搜索数量
                                        .similarityThreshold(0.5)//阈值
                                        .build()
                        )
                )
                .defaultTools(generatePptTool)
                .build();
    }

    //生成题目ai
    @Bean
    public ChatClient gnerateQuestionChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore, GenerateQuestionTool generateQuestionTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.generate_question_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new QuestionAnswerAdvisor(
                                vectorStore,//向量库
                                SearchRequest.builder()//搜索请求
                                        .topK(100)//搜索数量
                                        .similarityThreshold(0.5)//阈值
                                        .build()
                        ),
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .defaultTools(generateQuestionTool)
                .build();
    }

    @Bean
    public ChatClient markPaperClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore, GenerateQuestionTool generateQuestionTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.mark_open_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
//                        new MessageChatMemoryAdvisor(chatMemory)
                )
//                .defaultTools(generateQuestionTool)
                .build();
    }

    @Bean
    public ChatClient summaryExamClient(OpenAiChatModel model, ChatMemory chatMemory, SummaryExamTool summaryExamTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.summary_exam_ai)
                .defaultAdvisors(
//                        new SimpleLoggerAdvisor(),
//                        new QuestionAnswerAdvisor(
//                                vectorStore,//向量库
//                                SearchRequest.builder()//搜索请求
//                                        .topK(10)//搜索数量
//                                        .similarityThreshold(0.5)//阈值
//                                        .build()
//                        )
//                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .defaultTools(summaryExamTool)
                .build();
    }

    @Bean
    public ChatClient studnetChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore) {
        return ChatClient
                .builder(model)
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
                .defaultSystem(AiPromptConstant.student_chat_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),//支持日志记录
                        new MessageChatMemoryAdvisor(chatMemory),//记忆库
                        new QuestionAnswerAdvisor(
                                vectorStore,//向量库
                                SearchRequest.builder()//搜索请求
                                        .topK(100)//搜索数量
                                        .similarityThreshold(0.5)//阈值
                                        .build()
                        )
                )
                .build();
    }
    @Bean
    public ChatClient studentPracticeChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore, StudentPracticeTool studentPracticeTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.student_practice_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),//支持日志记录
                        new QuestionAnswerAdvisor(
                                vectorStore,//向量库
                                SearchRequest.builder()//搜索请求
                                        .topK(100)//搜索数量
                                        .similarityThreshold(0.5)//阈值
                                        .build()
                        )
                )
                .defaultTools(studentPracticeTool)
                .build();
    }
    @Bean
    public ChatClient learnPathAIChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore, CourseOptimizationTool courseOptimizationTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(AiPromptConstant.learn_path_ai)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()//支持日志记录
//                        new QuestionAnswerAdvisor(
//                                vectorStore,//向量库
//                                SearchRequest.builder()//搜索请求
//                                        .topK(100)//搜索数量
//                                        .similarityThreshold(0.5)//阈值
//                                        .build()
//                        )
                )
                .defaultTools(courseOptimizationTool)
                .build();
    }
}
